<?php

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 * Description of SubjectAction
 *
 * @author Administrator
 */
import('@.Service.AppService');
import('@.Service.SubjectService');
import('@.Service.SubjectContentService');
import('ORG.Util.Page');

class SubjectAction extends Action {

    

    private function _index($subjectService, $limit, $filterId, $searchName = null) {
        $count = $subjectService->countSubject($filterId, $searchName);

        $page = new Page($count, $limit);
        if (!is_null($searchName)) {
            $map['name'] = $searchName;
            foreach ($map as $key => $val) {
                $page->parameter .= "$key=" . urlencode($val) . "&";
            }
        }
        $show = $page->show();
        $list = $subjectService->listSubject($filterId, $page->firstRow . ', ' . $page->listRows, '*', $searchName);

        return array('show' => $show, 'list' => $list);
    }

    

    private function _science($subjectService, $id, $limit) {

        $scService = new SubjectContentService();
        $subject = $subjectService->findSubjectById($id);

        $count = $scService->countSubjectContentBySId($id);
        $page = new Page($count, $limit);
        $show = $page->show();
        $list = $scService->listSubjectContentBySId($id, $page->firstRow . ', ' . $page->listRows);

        return array('subject' => $subject, 'show' => $show, 'list' => $list);
    }

    

    /*     * *****管理台******* */

    function subjectManager() {
        $name = $_REQUEST['name'];
        $subjectService = new SubjectService();

        $data = $this->_index($subjectService, 10, 0, $name);

        $this->assign('name', $name);

        $this->assign('list', $data['list']);
        $this->assign('show', $data['show']);

        $this->assign('content', 'subject_manager');
        $this->display('Admin:global');
    }

    function scienceManager() {
        $id = $_REQUEST['id'];
        if (empty($id)) {

            $this->error('');
            return;
        }
        $subjectService = new SubjectService();
        $data = $this->_science($subjectService, $id, 10);

        $this->assign('subject', $data['subject']);
        $this->assign('list', $data['list']);
        $this->assign('show', $data['show']);

        $this->assign('content', 'science_manager');
        $this->display('Admin:global');
    }

    function contentManager() {
        $id = $_REQUEST['id'];
        if (empty($id)) {

            $this->error('');
            return;
        }
        $subjectService = new SubjectService();

        $scService = new SubjectContentService();
        $sc = $scService->findSubjectById($id);


        $this->assign('sc', $sc);
        //subject id
        $this->assign('subject', $subjectService->findSubjectById($_POST['sid']));
        $this->assign('content', 'content_manager');
        $this->display('Admin:global');
    }

    function subjectClearImages() {
        $id = $_POST['id'];
        if (empty($id)) {
            $this->ajaxReturn(null, ConstantUtil::$MOBILE_PARAMS_NONE, 0);
            return;
        }
        $scService = new SubjectContentService();
        $sc = $scService->findSubjectById($id);

        if (empty($sc)) {

            return;
        }

        if (empty($sc['content_img'])) {
            $this->ajaxReturn(null);
            return;
        }

        $sc['content_img'] = '';
        $scService->update($sc);

        $this->ajaxReturn(null);
    }

    /**
     * 添加Subject Content图片
     */
    function save() {
        $id = $_POST['id'];

        $images = $_POST['subject_content_images'];
        $scService = new SubjectContentService();

        $sc = $scService->findSubjectById($id);
        $sc['content_img'] = $images;

        $scService->update($sc);

        $this->redirect('Subject/scienceManager/id/' . $_POST['sid']);
    }

}

?>
